package com.example.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MenuActivity extends AppCompatActivity {
    private ImageView img;
    private Button btnPopupMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        img = findViewById(R.id.img);
        registerForContextMenu(img);  //给指定的View组件注册上下文菜单

        btnPopupMenu = findViewById(R.id.btn_popup_menu);
        btnPopupMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建PopupMenu
                PopupMenu popupMenu = new PopupMenu(MenuActivity.this, btnPopupMenu);
                //加载菜单资源pop_menu.xml
                popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());
                //菜单项事件监听
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.mi_qq:
                                Toast.makeText(MenuActivity.this, "分享到QQ空间", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.mi_friend:
                                Toast.makeText(MenuActivity.this, "分享到朋友圈", Toast.LENGTH_SHORT).show();
                                break;
                        }
                        return true;
                    }
                });

                //使用反射强制显示图标
                /*try {
                    Field field = popupMenu.getClass().getDeclaredField("mPopup");
                    field.setAccessible(true);
                    MenuPopupHelper mHelper = (MenuPopupHelper) field.get(popupMenu);
                    mHelper.setForceShowIcon(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }*/
                popupMenu.show();
            }
        });

    }

    /*选项菜单*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        try {
            //如果使用的是Androidx包，则使用"android.appcompat.view.menu.MenuBuilder"
            Class<?> menuClass = Class.forName("android.appcompat.view.menu.MenuBuilder");
            //Class<?> menuClass = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method menuMethod = menuClass.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            menuMethod.setAccessible(true);
            menuMethod.invoke(menu, true);  //通过反射设置菜单项的图标可见
        } catch (Exception e) {
            e.printStackTrace();
        }

        MenuItem show_item = menu.add(0, Menu.FIRST, 0, "显示").setIcon(R.mipmap.ic_launcher);
        //子菜单
        SubMenu shareMenu = menu.addSubMenu(0, Menu.FIRST + 1, 0, "分享").setIcon(R.mipmap.ic_launcher);
        shareMenu.setHeaderIcon(R.mipmap.ic_launcher);
        shareMenu.setHeaderTitle("分享到...");
        shareMenu.add(0, 100, 0, "微信");
        shareMenu.add(0, 101, 0, "QQ");
        shareMenu.add(0, 102, 0, "新浪微博");

        MenuItem detail_item = menu.add(0, Menu.FIRST + 2, 0, "详细");
        MenuItem save_item = menu.add(0, Menu.FIRST + 3, 0, "保存").setIcon(R.mipmap.ic_launcher);
        return true;
    }

    /*选项菜单事件*/
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                Toast.makeText(this, "你点击了显示菜单", Toast.LENGTH_SHORT).show();
                break;
            case Menu.FIRST + 1:
                Toast.makeText(this, "你点击了分享菜单", Toast.LENGTH_SHORT).show();
                break;
            case Menu.FIRST + 2:
                Toast.makeText(this, "你点击了详细菜单", Toast.LENGTH_SHORT).show();
                break;
            case Menu.FIRST + 3:
                Toast.makeText(this, "你点击了保存菜单", Toast.LENGTH_SHORT).show();
                break;
            case 100:
                Toast.makeText(this, "你分享到微信", Toast.LENGTH_SHORT).show();
                break;
            case 101:
                Toast.makeText(this, "你分享到QQ", Toast.LENGTH_SHORT).show();
                break;
            case 102:
                Toast.makeText(this, "你分享到新浪微博", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }

    /*上下文菜单*/
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        switch (v.getId()) {
            case R.id.img:
                menu.setHeaderIcon(R.drawable.img04);
                menu.setHeaderTitle("分享到...");
                menu.add(0, 200, 0, "QQ空间");
                menu.add(0, 201, 0, "朋友圈");
                SubMenu subMenu = menu.addSubMenu(0, 202, 0, "微博...");
                subMenu.add(0, 203, 0, "腾讯微博");
                subMenu.add(0, 204, 0, "新浪微博");
                break;
        }
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /*上下文菜单事件*/
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case 200:
                Toast.makeText(this, "分享到QQ空间", Toast.LENGTH_SHORT).show();
                break;
            case 201:
                Toast.makeText(this, "分享到朋友圈", Toast.LENGTH_SHORT).show();
                break;
            case 203:
                Toast.makeText(this, "分享到腾讯微博", Toast.LENGTH_SHORT).show();
                break;
            case 204:
                Toast.makeText(this, "分享到新浪微博", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}